package com.erpanderp.wam.asset.view;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.erpanderp.wam.asset.model.entity.AssetRequest;
import com.erpanderp.wam.asset.model.service.AssetRequestDAO;
import com.erpanderp.wam.usm.model.entity.user.WAMUser;
import com.erpanderp.wam.util.CommonLookup;

@ManagedBean
@ViewScoped
public class RecommenderInprocessAssetsBean implements Serializable{

	
	private static final long serialVersionUID = 1L;
	HttpSession session = (HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(true);
	private WAMUser wamUser = (WAMUser) session.getAttribute(CommonLookup.SESSION_ATTRIBUTE_WAMUSER);
	HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
	ServletContext context = (ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext();
	private TimeZone timeZone = TimeZone.getDefault();
	
	private List<AssetRequest> assetRequestInprocess = new ArrayList<AssetRequest>();
	private List<AssetRequest> assetRequestRecommended = new ArrayList<AssetRequest>();
	private List<AssetRequest> assetRequestRejected = new ArrayList<AssetRequest>();
	
	private AssetRequestDAO assetRequestDAO =new AssetRequestDAO();
	
	private int inprocessSNo=1;
	private int recommendedSNo=1;
	private int rejectedSNo=1;

	public RecommenderInprocessAssetsBean() {
		
		assetRequestInprocess = assetRequestDAO.getAssetRequestsByRecommenderId(wamUser.getEmployee().getEmployeeId(), CommonLookup.STATUS_INPROCESS);
		for(AssetRequest assetRequest  : assetRequestInprocess){
			assetRequest.setSno(inprocessSNo);
			inprocessSNo++;
		}
		
		assetRequestRecommended = assetRequestDAO.getAssetRequestsByRecommenderId(wamUser.getEmployee().getEmployeeId(), CommonLookup.STATUS_RECOMMENDED);
		for(AssetRequest assetRequest  : assetRequestRecommended){
			assetRequest.setSno(recommendedSNo);
			recommendedSNo++;
		}
		
		assetRequestRejected = assetRequestDAO.getAssetRequestsByRecommenderId(wamUser.getEmployee().getEmployeeId(), CommonLookup.STATUS_REJECTED);
		for(AssetRequest assetRequest  : assetRequestRejected){
			assetRequest.setSno(rejectedSNo);
			rejectedSNo++;
		}		
	}
	
	//Recommended by recommender 
	 public String assetRequestRecommendAction() {		 
	 try{
		  Map<String,String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		  String assetCode = params.get("assetCode");
		  String activity = params.get("activity");
		  FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("assetCode", assetCode);
		  FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("activity", activity);	
		  
		  FacesContext.getCurrentInstance().getExternalContext().redirect("/wam/asset/approveassetrequest.html?assetCode="+assetCode+"&activity="+activity);
		
		  }catch (Exception e) {
			  e.printStackTrace();
		   }
		 return null;	 
	}
	
	//SETTERS AND GETTERS
	public TimeZone getTimeZone() {
		return timeZone;
	}

	public void setTimeZone(TimeZone timeZone) {
		this.timeZone = timeZone;
	}


	public List<AssetRequest> getAssetRequestInprocess() {
		return assetRequestInprocess;
	}


	public void setAssetRequestInprocess(List<AssetRequest> assetRequestInprocess) {
		this.assetRequestInprocess = assetRequestInprocess;
	}


	public List<AssetRequest> getAssetRequestRecommended() {
		return assetRequestRecommended;
	}


	public void setAssetRequestRecommended(List<AssetRequest> assetRequestRecommended) {
		this.assetRequestRecommended = assetRequestRecommended;
	}


	public List<AssetRequest> getAssetRequestRejected() {
		return assetRequestRejected;
	}


	public void setAssetRequestRejected(List<AssetRequest> assetRequestRejected) {
		this.assetRequestRejected = assetRequestRejected;
	}


	public int getInprocessSNo() {
		return inprocessSNo;
	}


	public void setInprocessSNo(int inprocessSNo) {
		this.inprocessSNo = inprocessSNo;
	}


	public int getRecommendedSNo() {
		return recommendedSNo;
	}


	public void setRecommendedSNo(int recommendedSNo) {
		this.recommendedSNo = recommendedSNo;
	}


	public int getRejectedSNo() {
		return rejectedSNo;
	}


	public void setRejectedSNo(int rejectedSNo) {
		this.rejectedSNo = rejectedSNo;
	}


	public AssetRequestDAO getAssetRequestDAO() {
		return assetRequestDAO;
	}


	public void setAssetRequestDAO(AssetRequestDAO assetRequestDAO) {
		this.assetRequestDAO = assetRequestDAO;
	}

}
